﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
//using SIFF.Entity;
using BarcodeLib;
using System.Drawing.Imaging;

public partial class ApplyUser_BarCodeImage1 : System.Web.UI.Page
{
    protected string id
    {
        get
        {
            if (Request.QueryString["id"] != null)
                return Request.QueryString["id"];
            else
                return Guid.Empty.ToString();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Barcode b = new Barcode();
        System.Drawing.Image img = b.Encode(BarcodeLib.TYPE.CODE39, "0",Color.Black,Color.White,256,128);
        img = b.Generate_Labels(img);
        
        Bitmap bmp = new System.Drawing.Bitmap(256, 26, PixelFormat.Format1bppIndexed);
        Bitmap org = new Bitmap(img);
        Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
        BitmapData data = bmp.LockBits(r, ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
        IntPtr ptr = data.Scan0;
        int bytes = (bmp.Width * bmp.Height) / 8;
        byte[] rgbValues = new byte[bytes];

        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

        for (int counter = 0; counter < rgbValues.Length; counter += 1)
        {
            int total = 0;
            for (int i = 0; i < 8; i++)
            {
                int color = org.GetPixel((counter * 8 + i) % org.Width, (int)((counter * 8 + i) / org.Width)).R;
                if (color > 128)
                    total += (int)Math.Pow(2, 7 - i);
            }
            rgbValues[counter] = (byte)total;
        }


       // string fileName = Request.PhysicalApplicationPath + @"\BarcodeInner\" + FOBVT.Web.MainClass.SessionState.CurrentUser.CustomerID + ".pdf";
        //string imagePath = Path.Combine(Request.PhysicalApplicationPath, @"images\Name.bmp");
        // Copy the RGB values back to the bitmap
        System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

        bmp.UnlockBits(data);

        string path = Request.PhysicalApplicationPath + @"\BarcodeInner\Name.bmp";

        bmp.Save(path, ImageFormat.Bmp);
        //Bitmap bm = new Bitmap("C:\\Applications\\SIFF.FileStore\\BarCodeInner" + "\\" + id + ".bmp");


        //System.Drawing.Bitmap inputBmp = new Bitmap(img);
        //System.Drawing.Bitmap outputBmp = new Bitmap(196, 20);
        //System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(outputBmp);
        //g.DrawImage(inputBmp, new Point(1, 0));
        //Response.Clear();
        //bm.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Tiff);
        //Response.End();

    }

    protected string GenCode(string code)
    {
        switch (code)
        {
            case "电影节":
                return "F";
            case "电视节":
                return "T";
            case "影视两节":
                return "M";
            default:
                return "";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
 
}
